package cn.dong.edu.controller.admin;


import cn.dong.api.domain.CommonResult;
import cn.dong.edu.domain.Video;
import cn.dong.edu.domain.dto.VideoVo;
import cn.dong.edu.service.impl.VideoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/edu/video")
@Slf4j
//@CrossOrigin
public class VideoController {

    @Autowired
    private VideoServiceImpl videoService;



    /**
     * 查询这个章节下的所有小结（视频）
     *
     * @param chapterId
     * @return
     */
    @GetMapping("/findAllByChapterId/{chapterId}")
    public CommonResult findAllByChapterId(@PathVariable String chapterId) {
        List<VideoVo> list = videoService.findAllByChapterId(chapterId);
        return CommonResult.ok().data(list);
    }

    /**
     * 根据id查询小结
     *
     * @param videoId
     * @return
     */
    @GetMapping("/findById/{videoId}")
    public CommonResult findById(@PathVariable String videoId) {
        Video video = videoService.getById(videoId);
        return CommonResult.ok().data(video);
    }

    /**
     * 修改章节
     *
     * @param video
     * @return
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Video video) {
        return CommonResult.whether(videoService.updateById(video));
    }


    /**
     * 根据小结id，删除小结
     *
     * @param videoId
     * @return
     */
    @DeleteMapping("/delete/{videoId}")
    public CommonResult delete(@PathVariable("videoId") String videoId) {
        // 删除小结
        if (!videoService.delete(videoId)) {
            return CommonResult.error().message("删除课时失败!");
        } else {
            return CommonResult.ok();
        }
    }


    /**
     * 创建小结
     *
     * @param video
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Video video) {
        videoService.save(video);
        return CommonResult.ok();
    }




}

